package pl.practicas;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 * Unit test for simple App.
 */
public class AppTest2 
    extends TestCase
{
	String fichero="C:\\Users\\JAVI\\Clases\\PL\\Practicas\\practicas\\src\\test\\java\\pl\\practicas\\InvitacionT01.xml";
    /**
     * Create the test case
     *
     * @param testName name of the test case
     */
    public AppTest2( String testName )
    {
        super( testName );
    }

    /**
     * @return the suite of tests being tested
     */
    public static Test suite()
    {
    	//TestSuite suite= new TestSuite("Lectura y escritura");
        //suite.addTest(new AppTest("Fichero OK"));
        //return suite;
        return new TestSuite( AppTest2.class );
    }

    /**
     * Rigourous Test :-)
     */
    public void testApp2()
    {
    	try {
    		 
    		SAXParserFactory factory = SAXParserFactory.newInstance();
    		SAXParser saxParser = factory.newSAXParser();
    		
    		final WriteHTMLFile f = new WriteHTMLFile();
    	 
    		DefaultHandler handler = new DefaultHandler() {
    	 
    		boolean to = false;
    		boolean date = false;
    		boolean where = false;
    		boolean why = false;
    		boolean par = false;
    		boolean par2 = false;
    		boolean sign = false;
    		boolean emph = false;
    		boolean par3 = true;
    	 
    		public void startElement(String uri, String localName,String qName, 
    	                Attributes attributes) throws SAXException {
    	 
    			//System.out.println("Start Element :" + qName);
    	 
    			if (qName.equalsIgnoreCase("to")) {
    				to = true;
    			}
    	 
    			if (qName.equalsIgnoreCase("date")) {
    				date = true;
    			}
    	 
    			if (qName.equalsIgnoreCase("where")) {
    				where = true;
    			}
    	 
    			if (qName.equalsIgnoreCase("why")) {
    				why = true;
    			}
    			
    			if (qName.equalsIgnoreCase("par")) {
    				par = true;
    			}
    			
    			if (qName.equalsIgnoreCase("emph")) {
    				emph = true;
    			}
    			
    			if (qName.equalsIgnoreCase("signature")) {
    				sign = true;
    			}
    	 
    		}
    	 
    		public void endElement(String uri, String localName,
    			String qName) throws SAXException {
    	 
    			//System.out.println("End Element :" + qName);
    			if (par2) {
    				System.out.println("</P>");
    				f.par += "</P>";
    				par2=false;
    				par3=false;
    			}
    			if (qName.equalsIgnoreCase("emph")) {
    				System.out.print("</EM>");
    				f.par += "</EM>";
    				par2=true;
    			}
    			if (qName.equalsIgnoreCase("par")) {
    				if (par3){
    					System.out.println("\n</P>");
    					f.par += "\n</P>";
    				}
    				else {
    					par3=true;
    				}
    			}
    			if (qName.equalsIgnoreCase("invitation")) {
    				WriteHTMLFile.writeFile(f);
    			}
    	 
    		}
    	 
    		public void characters(char ch[], int start, int length) throws SAXException {
    	 
    			if (to) {
    				f.to="<TR><TD class=\"front\">To: </TD>\n<TD>" + new String(ch, start, length) + "</TD></TR>\n";
    				System.out.println(f.to);
    				to = false;
    			}
    	 
    			if (date) {
    				f.date="<TR><TD class=\"front\">When: </TD>\n<TD>" + new String(ch, start, length) + "</TD></TR>";
    				System.out.println(f.date);
    				date = false;
    			}
    	 
    			if (where) {
    				f.where="<TR><TD class=\"front\">Venue: </TD>\n<TD>" + new String(ch, start, length) + "</TD></TR>";
    				System.out.println(f.where);
    				where = false;
    			}
    	 
    			if (why) {
    				f.why="<TR><TD class=\"front\">Occasion: </TD>\n<TD>" + new String(ch, start, length) + "</TD></TR>";
    				System.out.println(f.why);
    				why = false;
    			}
    			
    			if (par) {
    				String p ="<P>" + new String(ch, start, length);
    				f.par += p;
    				System.out.println(p);
    				par = false;
    			}
    			
    			if (par2) {
    				String p2 = new String(ch, start, length);
    				f.par += p2;
    				System.out.println(p2);
    			}
    			
    			if (sign) {
    				f.sign="<P class=\"signature\">" + new String(ch, start, length) + "</P>";
    				System.out.println(f.sign);
    				sign = false;
    			}
    			
    			if (emph) {
    				String e= "<EM>" + new String(ch, start, length);
    				f.par += e;
    				System.out.println(e);
    				emph = false;
    			}
    	 
    		}
    	 
    	     };
    	 
    	       saxParser.parse("C:\\Users\\JAVI\\Clases\\PL\\Practicas\\practicas\\src\\main\\java\\pl\\practicas\\Invitacion.xml", handler);
    	 
    	     } catch (Exception e) {
    	       fail("No casa");
    	       e.printStackTrace();
    	     }
    }
   
}